package org.jeecg.modules.room.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.room.entity.RoomBookInfo;
import org.jeecg.modules.room.entity.RoomBookReceipt;
import org.jeecg.modules.room.mapper.RoomBookReceiptMapper;
import org.jeecg.modules.room.service.IRoomBookInfoService;
import org.jeecg.modules.room.service.IRoomBookReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 预定收款信息表
 * @Author: jeecg-boot
 * @Date:   2023-12-08
 * @Version: V1.0
 */
@Service
public class RoomBookReceiptServiceImpl extends ServiceImpl<RoomBookReceiptMapper, RoomBookReceipt> implements IRoomBookReceiptService {

    @Autowired
    private IRoomBookInfoService roomBookInfoService;

    /**
     * 保存预定收款
     * 收款只允许未确认、已确认和已分房。
     * 只有未确认时更改状态为已确认，其他不更新状态
     * @param roomBookReceipt
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReceipt(RoomBookReceipt roomBookReceipt) {
        try{
            RoomBookInfo roomBookInfo = roomBookInfoService.getOne(new QueryWrapper<RoomBookInfo>().eq("book_code",roomBookReceipt.getBookCode()));
            if(!roomBookInfo.getBookStatus().equals("00") && !roomBookInfo.getBookStatus().equals("10")&& !roomBookInfo.getBookStatus().equals("20")){
                throw new JeecgBootException("当前订单状态不能预定收款!");
            }

            JSONObject formData = new JSONObject();
            formData.put("prefix","SK");
            roomBookReceipt.setReceiptCode((String) FillRuleUtil.executeRule(FillRuleConstant.ORDER,formData));
            roomBookReceipt.setAuditFlag("0");
            this.baseMapper.insert(roomBookReceipt);
            //修改预订单状态
            if(roomBookInfo.getBookStatus().equals("00")) {
                roomBookInfo.setBookStatus("10");
                roomBookInfoService.update(roomBookInfo, new QueryWrapper<RoomBookInfo>().eq("book_code", roomBookReceipt.getBookCode()));
            }
        }
        catch (Exception e){
            throw e;
        }
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(String ids) {
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            for(String id : idList){
                RoomBookReceipt roomBookReceipt = this.baseMapper.selectById(id);
                if(roomBookReceipt.getAuditFlag().equals("1")){
                    throw new JeecgBootException(id + "该笔收款已经夜审不能删除！");
                }
            }
            this.baseMapper.deleteBatchIds(idList);
        }
        catch (Exception e){
            throw e;
        }
    }
}
